package com.hugoo.prt.slt;

import com.hugoo.core.Conf;
import com.hugoo.core.slt.AcFwd;
import com.hugoo.core.slt.Action;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Lovelock.Chan
 */
public class PrtAction extends Action {

    Map m = new HashMap();

    public AcFwd edit(HttpServletRequest request, HttpServletResponse response) {
        if (checkPara(request, response)) {
            PrtBuzz pb = new PrtBuzz(_sid, m);
            this.writeDataToClient(response, pb.gen(request,PrtBuzz.Type.EDIT));
        }
        return null;
    }

    public AcFwd view(HttpServletRequest request, HttpServletResponse response) {
        if (checkPara(request, response)) {
            PrtBuzz pb = new PrtBuzz(_sid, m);
            this.writeDataToClient(response, pb.gen(request,PrtBuzz.Type.VIEW));
        }
        return null;
    }

    public AcFwd save(HttpServletRequest request, HttpServletResponse response) {
        if (checkPara(request, response)) {
            String fv = request.getParameter("ReportFileValue");
            String fn = request.getParameter("ReportFileName");
            log.info("存储模板值为:" + fv);
            String pl = Conf.getBaseProp("prt.location");
            File plf = new File(pl);
            if (!plf.exists()) {
                plf.mkdirs();
            }
            plf = new File(pl + "/" + _sid + ".fr3");
            try {
                log.info("模板文件存储在：" + plf.getAbsolutePath());
                FileWriter fw = new FileWriter(plf);
                fw.write(fv);
                fw.close();
            } catch (IOException ex) {
                log.warn("写入打印模板出错！", ex);
            }
        }
        return null;
    }

    public AcFwd print(HttpServletRequest request, HttpServletResponse response) {
        if (checkPara(request, response)) {
            PrtBuzz pb = new PrtBuzz(_sid, m);
            this.writeDataToClient(response, pb.gen(request,PrtBuzz.Type.PRINT));
        }
        return null;
    }
}
